import java.util.List;
import java.util.Scanner;
import java.util.Stack;
import java.util.Vector;

public class Test {


    //栈的压入、弹出序列
    public boolean IsPopOrder (int[] pushV, int[] popV) {
        Stack<Integer>stack=new Stack<>();
        int j=0;//j是变化的不能循环后又等于0
        for (int i = 0; i < pushV.length; i++) {
            //入栈
            stack.push(pushV[i]);
            while (j<popV.length&&!stack.empty()&&stack.peek()==popV[j]){
                stack.pop();//出栈
                j++;//遍历下一个元素
            }
        }
        if(stack.empty()){
            return true;
        }
        return false;
    }
    //判断是左括号
    private static boolean isLeft(char tmp){
        if(tmp=='('||tmp=='['||tmp=='{'){
            return true;
        }
        return false;
    }
    //给定一个只包括 '('，')'，'{'，'}'，'['，']' 的字符串 s ，判断字符串是否有效。
    public static boolean isValid(String s) {
        //必须是偶数
        if(s.length()%2!=0)return false;
        Stack<Character> stack=new Stack<>();//存放左括号
        for (int i = 0; i < s.length(); i++) {
            char tmp=s.charAt(i);
            if(isLeft(tmp)){
                stack.push(tmp);
            }else{//右括号
                if(stack.empty())return false;//压根没有左括号，还来判断右括号
                switch (tmp){
                    case '}':
                        if(stack.pop()=='{'){
                         break;
                        }else{
                            return false;
                        }
                    case ')':
                        if(stack.pop()=='('){
                            break;
                        }else{
                            return false;
                        }
                    case ']':
                        if(stack.pop()=='['){
                            break;
                        }else{
                            return false;
                        }
                }

            }
        }
        if(stack.empty()){
            return true;
        }
        return false;
    }

    public static void main(String[] args) {
        System.out.println(isValid("){"));
    }
    private static boolean isOperator(String str){
        if(str.equals("+")||str.equals("-")||str.equals("*")||str.equals("/")){
            return true;
        }
        return false;
    }
    //后缀表达式计算结果
    public static int evalRPN(String[] tokens) {
        Stack<Integer>stack=new Stack<>();
        //判断一下字符串数组里面是否为运算符
        for (int i = 0; i < tokens.length; i++) {
            if(!isOperator(tokens[i])){//不是运算符时
                stack.push(Integer.parseInt(tokens[i]));
            }else{//遇到运算符时
                int right=stack.pop();
                int left=stack.pop();
                switch (tokens[i]){
                    case "+":
                        stack.push(left+right);
                        break;
                    case "-":
                        stack.push(left-right);
                        break;
                    case "*":
                        stack.push(left*right);
                        break;
                    case "/":
                        stack.push(left/right);
                        break;
                }
            }
        }
        return stack.pop();
    }
    //后缀表达式计算结果 这种只能操作个位数！
    public static int fun(String arr){
        Stack<Integer>stack=new Stack<>();
        for (int i = 0; i < arr.length(); i++) {
            char tmp=arr.charAt(i);
            if(tmp>=48&&tmp<=57){
                stack.push(tmp-48);
            }
            if(tmp=='+'||tmp=='-'||tmp=='*'||tmp=='/'){
                int number1=stack.pop();//右操作数
                int number2=stack.pop();//左操作数
                switch (tmp){
                    case '+':
                        stack.push(number2+number1);
                        break;
                    case '-':
                        stack.push(number2-number1);
                        break;
                    case '*':
                        stack.push(number2*number1);
                        break;
                    case '/':
                        stack.push(number2/number1);
                        break;
                }
            }
        }
        return stack.pop();
    }
    //递归实现单链表的逆序打印
    public static void main1(String[] args) {
        String[]arr=new String[]{"4","13","5","/","+"};
        System.out.println(evalRPN(arr));
    }
}
